package com.arges.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.arges.web.common.Constant;
import com.arges.web.common.GsonUtils;
import com.arges.web.common.SysEnum.Bllx;
import com.arges.web.common.SysEnum.Fhcd;
import com.arges.web.common.SysEnum.TNM;
import com.arges.web.common.SysEnum.Zlbw;
import com.arges.web.domain.bean.PatBean;
import com.arges.web.domain.pojo.Pthlgy;
import com.arges.web.page.PageInfo;
import com.arges.web.page.WebUtils;
import com.arges.web.service.PthlgyManager;

/**
 * doctor控制层
 * 
 * @author zxj
 *
 */
@Controller
@Scope("prototype")
@RequestMapping("/pthlgy")
public class PthlgyController
{

	private Logger LOGGER = LoggerFactory.getLogger(PthlgyController.class);

	@Autowired
	private PthlgyManager pthlgyManager;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getPagePthlgy.do")
	public String getPagePthlgy(PatBean patBean, HttpServletRequest request, ModelMap map)
	{

		PageInfo<PatBean> pageInfo = null;

		if (null == patBean.getMrn())
		{
			patBean.setMrn("");
		}

		if (null == patBean.getTumor_site())
		{
			patBean.setTumor_site("");
		}

		if (null == patBean.getDffrnttn())
		{
			patBean.setDffrnttn("");
		}
		if (null == patBean.getTnm())
		{
			patBean.setTnm("");
		}
		if (null == patBean.getPathology())
		{
			patBean.setPathology("");
		}
		if (null == patBean.getEgfr())
		{
			patBean.setEgfr("");
		}
		if (null == patBean.getSmoke())
		{
			patBean.setSmoke("");
		}
		String pageString = request.getParameter("PageInfo");
		if (StringUtils.isNotBlank(pageString))
		{
			pageInfo = GsonUtils.stringToObject(pageString, PageInfo.class);
		}
		else
		{
			pageInfo = new PageInfo<PatBean>();
		}

		try
		{
			PageInfo<Pthlgy> pojopInfo = WebUtils.beanToPojoPage(pageInfo, Pthlgy.class);
			pageInfo = pthlgyManager.getPagePthlgy(pojopInfo, patBean);
		}
		catch (Exception e)
		{
			System.out.println("getPagePthlgy is error. e=" + e);
		}
		map.put("chaxun", patBean.getMrn());
		map.put("zlbw", patBean.getTumor_site());
		map.put("fhcd", patBean.getDffrnttn());
		map.put("tnm", patBean.getTnm());
		map.put("bllx", patBean.getPathology());
		map.put("egfr", patBean.getEgfr());
		map.put("smoke", patBean.getSmoke());
		map.put("pthlgyBeanList", pageInfo.getResult());
		pageInfo.setResult(null);
		map.put("pthlgyPageInfo", GsonUtils.objectToJson(pageInfo));
		return "pat/pthlgy";
	}

	@RequestMapping(value = "/saveOrUpdate.do")
	@ResponseBody
	public String saveOrUpdate(HttpServletRequest request, PatBean patBean)
	{
		String result = Constant.SUCCESS;

		try
		{
			Pthlgy pthlgy = new Pthlgy();

			BeanUtils.copyProperties(patBean, pthlgy);
			pthlgy.setTumor_site(String.valueOf(Zlbw.valueOf(patBean.getTumor_site()).getId()));
			pthlgy.setDffrnttn(String.valueOf(Fhcd.valueOf(patBean.getDffrnttn()).getId()));
			pthlgy.setTnm(String.valueOf(TNM.valueOf(patBean.getTnm()).getId()));
			pthlgy.setPathology(String.valueOf(Bllx.valueOf(patBean.getPathology()).getId()));
			pthlgyManager.update(pthlgy);
		}
		catch (Exception e)
		{
			System.out.println("saveOrUpdate is error,error is" + e);
			result = Constant.ERROR;
		}

		return result;
	}

	/**
	 * 根据病历号获取患者信息
	 *
	 * @param mrn
	 * @return
	 */
	@RequestMapping(value = "/getPthlgyByMrn.do")
	@ResponseBody
	public PatBean getPthlgyByMrn(HttpServletRequest request)
	{
		String mrn = request.getParameter("mrn");
		PatBean patBean = null;
		try
		{
			patBean = pthlgyManager.getPthlgyByMrn(mrn);
		}
		catch (Exception e)
		{
			System.out.println("getPthlgyByMrn is error,error is" + e);
			patBean = null;
		}
		return patBean;
	}

	/**
	 * 获取肿瘤部位列表
	 * 
	 * @param request
	 * @return list
	 */
	@RequestMapping(value = "/getZlbwList.do")
	@ResponseBody
	public Zlbw[] getZlbwList()
	{
		return Zlbw.values();
	}

	/**
	 * 获取分化程度列表
	 * 
	 * @param request
	 * @return list
	 */
	@RequestMapping(value = "/getFhcdList.do")
	@ResponseBody
	public Fhcd[] getFhcdList()
	{
		return Fhcd.values();
	}

	/**
	 * 获取tnm分期列表
	 * 
	 * @param request
	 * @return list
	 */
	@RequestMapping(value = "/getTnmList.do")
	@ResponseBody
	public TNM[] getTnmList()
	{
		return TNM.values();
	}

	/**
	 * 获取病理类型列表
	 * 
	 * @param request
	 * @return list
	 */
	@RequestMapping(value = "/getBllxList.do")
	@ResponseBody
	public Bllx[] getBllxList()
	{
		return Bllx.values();
	}

}
